"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 表名大小写敏感
Case Name   : 表名大小写敏感结合dbname/schema_name
Create At   : 2022/09/09
@zou_jialiang0501120110
Description :
    1、create database
    2、create schema
    3、create table指定database
    4、insert插入数据
    5、select查询数据
    6、create table不指定database
    7、insert插入数据
    8、select查询数据
    9、清理环境
Expect      :
    1、创建成功
    2、创建成功
    3、创建成功
    4、成功
    5、成功
    6、创建成功
    7、成功
    8、成功
    9、清理环境成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class LowerSensitiveCase(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.db_name = "db_letter_0030"
        self.sch_name = "sch_letter_0030"
        self.tab_name_01 = "T_LETTER_0030_01"
        self.tab_name_02 = "T_LETTER_0030_02"
        self.guc_name = "dolphin.lower_case_table_names"

    def test_sensitive(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "创建数据库失败:" + step1_text)

        step2_text = "-----step2:create schema;expect:创建成功-----"
        self.logger.info(step2_text)
        sql_cmd2 = f'''drop schema if exists {self.sch_name};
                    create schema {self.sch_name};'''
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res2)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sql_res2,
                      "创建schema失败:" + step1_text)

        step3_text = "-----step3:create table指定database;expect:创建成功-----"
        self.logger.info(step3_text)
        sql_cmd3 = f'''set {self.guc_name} = 0;
                    drop table if exists 
                    {self.db_name}.{self.sch_name}.{self.tab_name_01};
                    create table 
                    {self.db_name}.{self.sch_name}.{self.tab_name_01} 
                    (col1 int
                    ,col2 varchar(64));'''
        self.logger.info(sql_cmd3)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res3)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res3, "设置参数失败:" + step3_text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      sql_res3, "建表失败:" + step3_text)

        step4_text = "-----step4:insert插入数据;expect:插入成功-----"
        self.logger.info(step4_text)
        sql_cmd4 = f'''set {self.guc_name} = 0;
                    insert into 
                    {self.db_name}.{self.sch_name}.{self.tab_name_01} values 
                    (1,'aa'),(2,'bb'),(1,'cc'),(4,'dd'),(1,'ee');'''
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res4)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res4, "设置参数失败:" + step4_text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_res4, "insert失败:" + step4_text)

        step5_text = "-----step5:select查询数据;expect:查询成功-----"
        self.logger.info(step5_text)
        sql_cmd5 = f'''set {self.guc_name} = 0;
                    select count(*) from 
                    {self.db_name}.{self.sch_name}.{self.tab_name_01};'''
        sql_res5 = self.commonsh.execut_db_sql(sql_cmd5,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res5)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res5, "设置参数失败:" + step5_text)
        self.assertIn("5", sql_res5, "查询失败:" + step5_text)

        step6_text = "-----step6:create table不指定database;expect:创建成功-----"
        self.logger.info(step6_text)
        sql_cmd6 = f'''set {self.guc_name} = 0;
                    drop table if exists {self.sch_name}.{self.tab_name_02};
                    create table {self.sch_name}.{self.tab_name_02} 
                    (col1 int
                    ,col2 varchar(64));'''
        self.logger.info(sql_cmd6)
        sql_res6 = self.commonsh.execut_db_sql(sql_cmd6,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res6)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res6, "设置参数失败:" + step6_text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      sql_res6, "建表失败:" + step6_text)

        step7_text = "-----step7:insert插入数据;expect:插入成功-----"
        self.logger.info(step7_text)
        sql_cmd7 = f'''set {self.guc_name} = 0;
                    insert into {self.sch_name}.{self.tab_name_02} values 
                    (1,'aa'),(2,'bb'),(1,'cc'),(4,'dd'),(1,'ee');'''
        sql_res7 = self.commonsh.execut_db_sql(sql_cmd7,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res7)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res7, "设置参数失败:" + step7_text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_res7, "insert失败:" + step7_text)

        step8_text = "-----step8:select查询数据;expect:查询成功-----"
        self.logger.info(step8_text)
        sql_cmd8 = f'''set {self.guc_name} = 0;
                    select count(*) from {self.sch_name}.{self.tab_name_02};'''
        sql_res8 = self.commonsh.execut_db_sql(sql_cmd8,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res8)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      sql_res8, "设置参数失败:" + step8_text)
        self.assertIn("5", sql_res8, "查询失败:" + step8_text)

    def tearDown(self):
        step9_text = "-----step9:清理环境;expect:成功-----"
        self.logger.info(step9_text)

        drop_tab_text = "-----drop table-----"
        self.logger.info(drop_tab_text)
        drop_tab_cmd = f'''set {self.guc_name} = 0;
                    drop table if exists 
                    {self.db_name}.{self.sch_name}.{self.tab_name_01} cascade;
                    drop table if exists 
                    {self.sch_name}.{self.tab_name_02} cascade;'''
        self.logger.info(drop_tab_cmd)
        drop_tab_res = self.commonsh.execut_db_sql(drop_tab_cmd,
                                                   dbname=f'{self.db_name}')
        self.logger.info(drop_tab_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG,
                      drop_tab_res, "设置参数失败:" + step9_text)
        self.assertTrue(drop_tab_res.count(self.constant.TABLE_DROP_SUCCESS)
                        == 2, "删表失败" + drop_tab_text)

        drop_sch_text = "-----drop schema-----"
        self.logger.info(drop_sch_text)
        drop_sch_cmd = f'''drop schema if exists {self.sch_name} cascade;'''
        self.logger.info(drop_sch_cmd)
        drop_sch_res = self.commonsh.execut_db_sql(drop_sch_cmd,
                                                   dbname=f'{self.db_name}')
        self.logger.info(drop_sch_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG,
                      drop_sch_res, "删除schema失败" + drop_sch_res)

        drop_db_text = "-----drop database-----"
        self.logger.info(drop_db_text)
        drop_db_cmd = f'''drop database if exists {self.db_name};'''
        self.logger.info(drop_db_cmd)
        drop_db_res = self.commonsh.execut_db_sql(drop_db_cmd)
        self.logger.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_db_res, "删除database失败" + drop_db_res)

        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
